import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {
  // 获取规则校验分页
  getInvoiceChecks(params) {
    return httpFetch.get('/api/receipt/check/config', params)
      .catch(err => {
        errorMessage(err.response);
      });
  },

  // 删除规则校验
  deleteInvoiceCheck(id) {
    return httpFetch.delete(`/api/receipt/check/config/${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('delete error');
      });
  },
  // 新建规则校验
  newInvoiceCheck(params) {
    return httpFetch.post('/api/receipt/check/config', params)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('post error');
      });
  },
  // 获取抬头校验
  getHeadUpVerification(id) {
    return httpFetch.get(`/api/receipt/check/title/${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('get error');
      });
  },
  // 保存抬头校验
  saveHeadUpVerification(params) {
    return httpFetch.post(`/api/receipt/check/title`, params)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('get error');
      });
  },
  // 获取连号校验
  getConsecutiveVerification(id) {
    return httpFetch.get(`/api/receipt/check/consecutive/${id}`)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('get error');
      });
  },
  // 保存连号校验
  saveConsecutiveVerification(params) {
    return httpFetch.post(`/api/receipt/check/consecutive`,params)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('get error');
      });
  },
  // 获取规则校验分页
  getOverdueList(id, params) {
    return httpFetch.get(`/api/receipt/check/overtime/${id}`, params);
  },
  // 保存规则校验
  submitExpirationReminder(values, requestBody, companyOID, id) {
    const { sequence, enabled, description, receiptTypeList, taxCodeList, expiration, effectiveTime, forceEnabled, expiryRemindDay } = values;
    const { staticCalibrationFlag, expiryDay, dynamicRuleFailureTime } = expiration;
    const dynamicRuleFailureArray = !staticCalibrationFlag && dynamicRuleFailureTime.split('-');
    const effectiveArray = effectiveTime.split('-');
    const effectiveMonth = effectiveArray[0];
    const effectiveDay = effectiveArray[1];
    let dynamicRuleFailureMonth,
      dynamicRuleFailureDay;
    let companyOid = companyOID;
    if (dynamicRuleFailureArray) {
      dynamicRuleFailureMonth = (dynamicRuleFailureArray[0]);
      dynamicRuleFailureDay = (dynamicRuleFailureArray[1]);
    }
    requestBody = {
      ...requestBody,
      companyOid,
      sequence,
      enabled,
      description,
      receiptTypeList,
      taxCodeList,
      forceEnabled,
      staticCalibrationFlag,
      dynamicRuleFailureMonth,
      dynamicRuleFailureDay,
      effectiveMonth,
      effectiveDay,
      expiryDay,
      dynamicRuleFailureTime,
      effectiveTime,
      expiryRemindDay
    };
    return new Promise((resolve, reject) => {
      httpFetch.post(`/api/receipt/check/overtime/${id}`, requestBody)
        .then(() => {
          resolve(true);
        })
        .catch(e => {
          errorMessage(e.response);
          reject(false);
        });
    });
  },
  // 删除逾期校验规则
  deleteExpirationReminder(id) {
    return httpFetch.delete(`/api/invoice/overtime?id=${id}`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('delete error');
      });
  },
  // 根据发票ID获取校验规则
  getInvoiceCheckDetails(id) {
    return httpFetch.get(`/api/receipt/check/config/${id}`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  // 保存校验规则
  saveInvoiceCheckDetails(params) {
    return httpFetch.post(`/api/receipt/check/config`, params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('post error');
      });
  },
  // 获取发票生成费用规则
  getInvoiceToCost(params){
    return httpFetch.get(`/api/receipt/to/invoice/config`, params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  // 删除发票生成费用规则
  deleteInvoiceToCost(id) {
    return httpFetch.delete(`/api/receipt/to/invoice/config/${id}`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('delete error');
      });
  },
  // 新建发票生成费用规则
  newInvoiceToCost(params) {
    return httpFetch.post('/api/receipt/to/invoice/config', params)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('post error');
      });
  },
  // 新建发票生成费用规则详情
  getInvoiceToCostDetail(receiptToInvoiceOptId) {
    return httpFetch.get(`/api/receipt/to/invoice/config/${receiptToInvoiceOptId}`)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('get error');
      });
  },
  // 获取发票控件字段
  getInvoiceControl(params){
    return httpFetch.get(`/api/receipt/display/config`, params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  // 删除发票控件字段
  deleteInvoiceControl(id) {
    return httpFetch.delete(`/api/receipt/display/config/${id}`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('delete error');
      });
  },
  // 新建发票控件字段
  newInvoiceControl(params) {
    return httpFetch.post('/api/receipt/display/config', params)
      .catch(err => {
        errorMessage(err.response);
        throw new Error('post error');
      });
  },
  // 删除发票控件字段
  getInvoiceControlDetails(id, params) {
    return httpFetch.get(`/api/receipt/display/config/${id}`,params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  // 恢复默认设置
  resetInvoiceControlDetails(params) {
    return httpFetch.get(`/api/receipt/display/config/reset`,params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  //获取当前最高优先级
  highestPriority(){
    return httpFetch.get(`/api/receipt/to/invoice/config/highestPriority`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  // 根据发票ID获取校验规则
  getComplianceDetails(id) {
    return httpFetch.get(`/api/receipt/check/compliance/${id}`)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  },
  saveComplianceDetails(params) {
    return httpFetch.post(`/api/receipt/check/compliance`, params)
      .catch(e => {
        errorMessage(e.response);
        throw new Error('get error');
      });
  }
};
